<!-- 主页 -->
<template>
    <!--  <div class="home-container">
        
    </div> -->
    <el-container class="container-home">
        <!-- 头部区域 -->
        <el-header>
            <h1>电商管理项目</h1>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <el-container>
            <!-- 侧边栏 -->
            <el-aside :width="iscollapse ? '64px' : '200px'">
                <div class="collapse" @click="changeCollapse">|||</div>
                <el-menu background-color="#313743" text-color="#fff" active-text-color="#ffd04b" unique-opened :collapse="iscollapse" :collapse-transition="false" router :default-active="NavState">
                    <!-- 需要给所有index添加唯一index -->
                    <!-- 使用v-for必须要绑定唯一的key -->
                    <!-- 一级菜单 -->
                    <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
                        <!-- 菜单模板区域 -->
                        <template slot="title">
                            <i :class="aside_icon[item.id]"></i>
                            <span>{{item.authName}}</span>
                        </template>
                        <!-- 二级菜单 -->
                        <el-menu-item :index="'/'+itemn.path" v-for="itemn in item.children" :key="itemn.id" @click="saveNavState('/'+itemn.path)">
                            <template slot="title">
                                <i class="el-icon-menu"></i>
                                <span>{{itemn.authName}}</span>
                            </template>
                        </el-menu-item>
                    </el-submenu>
                </el-menu>

            </el-aside>
            <!-- 主题区域 -->
            <el-main>
                <!-- 占位 -->
                <router-view></router-view>
            </el-main>
        </el-container>
    </el-container>
</template>

<script>
    export default {
        //生命周期函数，在页面被打开时调用
        created(){
            this.getMenuList()
            this.NavState = window.sessionStorage.getItem('navstate')
        },
        data() {
            return {
                // 保存侧边栏内容
                menuList:[],
                // 侧边栏图标
                aside_icon:{
                125:'icon-users iconfont',
                103:'icon-quanxian iconfont',
                101:'icon-shangpin iconfont',
                102:'icon-dingdan iconfont',
                145:'icon-shuju iconfont'},
                // 只打开一个菜单 存储打开状态
                iscollapse:false,
                // 高亮显示
                NavState:''
            }
        },
        methods: {
            //退出功能
            logout() {
                window.sessionStorage.clear()
                this.$router.push('/login')
            },
            // 获取侧边栏数据
            async getMenuList() {
                const {data:res} = await this.$http.get('menus')
                this.menuList = res.data
            },
            // 只打开一个菜单
            changeCollapse(){
                this.iscollapse = !this.iscollapse
            },
            // 高亮显示的nav
            saveNavState(navstate) {
                window.sessionStorage.setItem('navstate',navstate)
                this.NavState = navstate
            }
        }
    }
</script>
<style scoped>
    /* @import url(); 引入css类 */
    .container-home {
        height: 100%;
    }

    .el-header {
        display: flex;
        justify-content: space-between;
        background-color: #353c3f;
    }

    .el-header h1 {
        color: #ffffff;
        line-height: 60px;
        font-size: 20px
    }

    .el-aside {
        background-color: #313743;
    }
    .el-aside .collapse {
        height: 30px;
        background-color: #777;
        line-height: 30px;
        text-align: center;
        cursor: pointer;
        font-size: 16px;
        word-spacing: 10px;
        color:#fff
    }
    .el-aside .el-menu {
        border-right: none
    }

    .el-main {
        background-color: #e9edf1;
    }
    .iconfont {
        margin-right: 10px
    }
</style>